package com.example.demo3.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsbj.common.msg.DataPageMessage;
import com.dsbj.common.msg.ResponseMessage;
import com.example.demo3.entity.ResourceDevice;
import com.example.demo3.service.ResourceDeviceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("resourceDevice")
public class ResourceDeviceController extends BaseController{

    @Resource
    private ResourceDeviceService resourceDeviceService;
    
    @RequestMapping("list")
    public void list(){}

    @RequestMapping("/resourceDeviceInfo")
    @ResponseBody
    public DataPageMessage resourceDeviceInfo(int pageindex, String orderdir, int pagesize, String ordercolumn, int sEcho,String resourceDeviceNo,String resourceDeviceName){
         //int sEchoInt = 1;
      /* if(StringUtils.isBlank(pageindex)){
            pageindex ="1";
        }
       if(StringUtils.isBlank(pagesize)){
            pagesize ="15";
        }
        if(StringUtils.isBlank(orderdir)){
            orderdir ="desc";
        }

        Map<Object,Object> map = new HashMap<>();
            map.put("pageindex",pageindex);
            map.put("pagesize",pagesize);
            map.put("orderdir",orderdir);
            map.put("ordercolumn",ordercolumn);
        int countPage=resourceDeviceService.countPage(map);
        List<ResourceDevice> resourceDeviceList=resourceDeviceService.queryPage(map);
        return DataPageMessage.ok(resourceDeviceList,countPage,countPage,sEcho,"");*/
        Page<ResourceDevice> resourceDevicePage =resourceDeviceService.getSelectPage(resourceDeviceNo,resourceDeviceName,pageindex,pagesize);
        if(resourceDevicePage==null||resourceDevicePage.getRecords()==null){
            return DataPageMessage.ok(null,(int)resourceDevicePage.getCurrent(),(int)resourceDevicePage.getCurrent(),sEcho,"");
        }
        return DataPageMessage.ok(resourceDevicePage.getRecords(),(int)resourceDevicePage.getTotal(),(int)resourceDevicePage.getTotal(),sEcho,"");
    }

    @RequestMapping("add")
    public String add (){
        return "resourceDevice/add";
    }

    @RequestMapping("edit")
    public String edit (Model model,String id){
        if(StringUtils.isNotBlank(id)){
            ResourceDevice resourceDevice = resourceDeviceService.get(id);
            model.addAttribute("resourceDevice",resourceDevice);
        }
        return "resourceDevice/edit";
    }

    @RequestMapping("del")
    @ResponseBody
    public ResponseMessage del (String id){
           resourceDeviceService.deleteById(id);
      return ResponseMessage.ok("删除成功");
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public ResponseMessage saveOrUpdate(ResourceDevice resourceDevice){
        resourceDevice.setCreateUser("xsyuan");
        String msg= "";
        if(StringUtils.isNotBlank(resourceDevice.getId())){
            resourceDeviceService.update(resourceDevice);
            msg = "修改成功";
        }else{
            resourceDeviceService.add(resourceDevice);
            msg = "添加成功";
        }
        return ResponseMessage.ok(msg);
    }

}
